/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.reminder.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.common.object.Directory;
import etcore.webapp.grouptalk.common.service.DirectoryService;

/**
 * This action validates the request and executes the command of getting
 * reminder records,then set the corresponding attributes for output
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class RemindersAction extends AbstractAction {

	private static final long serialVersionUID = 4790298712581806445L;
	// validate result
	public static final int START = 1;

	// utility constants

	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return START;

	}

	@Override
	public String getForward(HttpServletRequest request, int validationResult) {

		return PageConstants.PAGE_PATH_REMINDERS;
	}

	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {
		// enable banner or not
		String hasBanner = request.getParameter(PageConstants.HASBANNER);
		if (Messages.FALSE.equals(hasBanner))
			request.setAttribute(PageConstants.HASBANNER, false);
		else
			request.setAttribute(PageConstants.HASBANNER, true);

		// enable footer or not
		String hasFooter = request.getParameter(PageConstants.HASFOOTER);
		if (Messages.FALSE.equals(hasFooter))
			request.setAttribute(PageConstants.HASFOOTER, false);
		else
			request.setAttribute(PageConstants.HASFOOTER, true);
		getDirectories(request);
	}

	private void getDirectories(HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		String lang = (String) session.getAttribute(Messages.LANG);
		DirectoryService directoryService = new DirectoryService();
		List<Directory> directories = null;
		if (Messages.LANG_LOCAL.equals(lang)) {
			directories = directoryService.findLocalDirsByParent(-4);
		}else{
			directories = directoryService.findEnglishDirsByParent(-4);
		}
		
		if (directories != null) {
			request.setAttribute("directories", directories);
		}

	}

	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

}
